# _*_ coding: utf-8 _*_
from odoo import models, fields, api


class BomProductSubstitution(models.Model):
    _name = "bom.product.substitution"
    _rec_name = "subt_product_id"

    bom_line_id = fields.Many2one("mrp.bom.line", string="Bom Line", readonly=True, required=True)
    bom_line_product_qty = fields.Float(string="Bom Line Product Quantity",
                                        related="bom_line_id.product_qty", readonly=True)
    bom_line_product_uom = fields.Many2one("product.uom",
                                           related="bom_line_id.product_uom_id",
                                           readonly=True)
    subt_product_id = fields.Many2one("product.product", string="Substitution Product",
                                      required=True, domain="[('uom_id','=',bom_line_product_uom)]")
    subt_product_uom_id = fields.Many2one("product.uom",
                                          related="subt_product_id.uom_id",
                                          readonly=True)
    subt_product_qty = fields.Float(string="Quantity")

    _sql_constraints = [
        ('subt_product_id_uniq', 'unique (bom_line_id,subt_product_id)', u'Substitution Product Already Exists!'),
        ("subt_product_qty", "check(subt_product_qty>=0)", u"subt_product_qty >= 0 !")
    ]
